% (1+1)-ES Slide 61
% Michael Sieber, Philipp Rusch

sum = 0;
iterations = 100

for i=1:iterations

	%initialization (line 1)
	N = 310;
	parent = floor(mod(randn(N, 1), 2)); % B^n random number

	% determine initial parent fitness (line 2)
	parent_fitness = F(parent);

	% generation counter (line 3)
	g = 0;

	% evolution loop (line 4)
	while(parent_fitness != N)
		% calculate mutation bit flip
		mutation = zeros(N, 1);
		rnd = floor(RandBetween(1, N, 1, 1));
		mutation(rnd) = 1;
		
		% calculate offsprings (line 5-6)
		offspring = floor(mod(parent + mutation, 2));
		offspring_fitness = F(offspring);
		
		% minimization (line 7-10)
		if(offspring_fitness >= parent_fitness)
			parent = offspring;
			parent_fitness = offspring_fitness;
		endif
		
		% generation increase (line 11)
		g = g+1;
	endwhile
	
	sum = sum + g;

endfor

mean = sum / iterations